package xpn.platform.modules.login;

import javax.servlet.http.HttpServletRequest;

import xpn.platform.common.exception.XpnAuthenticationException;

/**
 * 登录工具：从http请求获取用户名和密码
 * 
 * @author bobatkm Sep 4, 2017
 *
 */
public class LoginUtil {

	/**
	 * 获取登录用户名
	 * 
	 * @param userName
	 * @param request
	 * @return
	 */
	public static String getUserName(String userName, HttpServletRequest request) {
		if (userName == null || "".equals(userName)) {
			userName = request.getParameter("username");
		}

		if (userName == null || "".equals(userName)) {
			userName = request.getHeader("username");
		}

		if (userName == null || "".equals(userName)) {
			throw new XpnAuthenticationException("LoginUtil : User name can not be empty!");
		}

		return userName;
	}

	/**
	 * 获取登录密码
	 * 
	 * @param password
	 * @param request
	 * @return
	 */
	public static String getPassword(String password, HttpServletRequest request) {
		if (password == null || "".equals(password)) {
			password = request.getParameter("password");
		}

		if (password == null || "".equals(password)) {
			password = request.getHeader("password");
		}

		if (password == null || "".equals(password)) {
			throw new XpnAuthenticationException("LoginUtil : Password can not be empty!");
		}

		return password;
	}
}
